package com.weipu.common.facade.support.service;

import java.util.ArrayList;

import android.content.Intent;

import com.weipu.common.DXApplication;
import com.weipu.common.facade.support.service.task.BaseServiceTask;
import com.weipu.common.util.Logger;

public final class TaskExecutor
{
    private static final String TAG = "TaskExecutor";
    
    /**
     * 后台执行任务
    * @param task 任务对象
    */
    @SuppressWarnings({"rawtypes"})
    public static void doTaskInBackground(BaseServiceTask task)
    {
        Logger.d(TAG, "doTaskInBackground, BaseServiceTask: " + task);
        
        Intent intent = new Intent(DXApplication.getContext(), TaskService.class);
        intent.putExtra(BaseServiceTask.TASK_DATA, task);
        DXApplication.getContext().startService(intent);
        
    }
    
    /**
     * 后台执行任务
     * @param task 任务对象
     * @param isNeedBroadcast2UI 数据回调完成后是否需要向UI通知(并传递结果数据)
     * @param whatForUICallback 对于同一场景区分多个回调任务(类似 msg.what)
     */
    @SuppressWarnings({"rawtypes"})
    public static void doTaskInBackground(BaseServiceTask task, boolean isNeedBroadcast2UI, int whatForUICallback)
    {
        Logger.d(TAG, "doTaskInBackground, isNeedBroadcast2UI: " + isNeedBroadcast2UI + ", whatForUICallback: "
            + whatForUICallback);
        
        Intent intent = new Intent(DXApplication.getContext(), TaskService.class);
        intent.putExtra(BaseServiceTask.TASK_DATA, task);
        task.setNeedBroadcast2UI(isNeedBroadcast2UI);
        task.setWhatForUICallback(whatForUICallback);
        DXApplication.getContext().startService(intent);
    }
    
    /**
     * 后台执行任务
    * @param taskList 任务对象列表
    */
    @SuppressWarnings({"rawtypes"})
    public static void doTaskInBackground(ArrayList<BaseServiceTask> taskList)
    {
        Logger.d(TAG, "doTaskInBackground, taskList.size: " + (null != taskList ? taskList.size() : "null"));
        Intent intent = new Intent(DXApplication.getContext(), TaskService.class);
        intent.putExtra(BaseServiceTask.TASK_DATA, taskList);
        DXApplication.getContext().startService(intent);
    }
}
